#include <cassert>
#include "MioString.h"

namespace mio
{
	namespace str
	{
		std::vector<std::string> split(const std::string &str, const std::string &delim)
		{
			std::vector<std::string> tokenList;

			if(str.length() == 0)
				return tokenList;	//문자열이 없으면 짜른것은 비었을 것이닫

			std::string token;
			for(std::size_t i = 0 ; i < str.length() ; i++)
			{
				const char ch = str[i];
				size_t found = delim.find_first_of(ch);
				if(found == std::string::npos)
				{
					token.push_back(ch);
				}
				else
				{
					//문자열 구성
					if(token.length() > 0)
						tokenList.push_back(token);
					token.clear();
				}
			}
			//남은 토큰 확인
			if(token.length() > 0)
				tokenList.push_back(token);
			return tokenList;
		}
		std::vector<std::string> split(const std::string &str, const char *delim)
		{
			assert(delim != NULL);
			std::string delimStr(delim);
			return split(str, delimStr);
		}
		std::string trim(const std::string &str)
		{
			return rightTrim(leftTrim(str));
		}
		std::string leftTrim(const std::string &str)
		{
			std::size_t n = str.find_first_not_of(mio::str::whitespace);
			return n == std::string::npos ? str : str.substr(n, str.length());
		}
		std::string rightTrim(const std::string &str)
		{
			std::size_t n = str.find_last_not_of(mio::str::whitespace);
			return n == std::string::npos ? str : str.substr(0, n + 1);
		}
	}
	std::string StringUtil::trim(const std::string &str) const
	{
		return mio::str::trim(str);
	}
	std::string StringUtil::leftTrim(const std::string &str) const
	{
		return mio::str::leftTrim(str);
	}
	std::string StringUtil::rightTrim(const std::string &str) const
	{
		return mio::str::rightTrim(str);
	}
	std::vector<std::string> StringUtil::split(const std::string &str, const char *delim) const
	{
		return mio::str::split(str, delim);
	}
}
